// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var leftMenu = require("../leftMenu/leftMenu");
	var topMenu = require("../topMenu/topMenu");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var validate = require("validate");
	var databind = require("databind");
	var css_deliveryNotice = require("./followInfoDetail.css");
	var html_addFollowInfo = require("./html_addFollowInfo.html");
	var tools =require("tools");
	var constant = require("constant");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var followId;
	var init = function(page) {
		followId = page.params.followId;
		inquireFinanceFollowPersonDetail();
//		leftMenu.init(6);
		initEvent();
		router.init();
		
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}
	};

	var inquireFinanceFollowPersonDetail = function(){
		base.fnAjax({
			action: "inquireFinanceFollowPersonDetail.json",
			oData: {
				followId: followId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					
					var tpl = $("#followInfoDetail .tpl").html();
					var html = juicer(tpl, data);
					$("#followInfoDetail .body").html(html);
					
					$.each($(".visitFile"), function() {
						var $this = $(this);
						var h = $this.html().split(',');
						$this.html("");
						$.each(h, function() {
							var str = '<a target="_blank" href="/finance/api/v1.0/downloadFile.file?fileUuid='+this+'">'+tools.inquireFile(this)+'</a>';
							$this.append(str);
						});
					});
					
					$.each($(".visitImage"), function() {
						var $this = $(this);
						var h = $this.html().split(',');
						$this.html("");
						$.each(h, function() {
							var str = '<a target="_blank" href="/finance/api/v1.0/downloadFile.file?fileUuid='+this+'">'+tools.inquireFile(this)+'</a>';
							$this.append(str);
						});
					});
					
					$.each($(".visitAudio"), function() {
						var $this = $(this);
						var h = $this.html().split(',');
						$this.html("");
						$.each(h, function() {
							var str = '<a target="_blank" href="/finance/api/v1.0/downloadFile.file?fileUuid='+this+'">'+tools.inquireFile(this)+'</a>';
							$this.append(str);
						});
					});
					
					$("#followInfoDetail .submit").unbind("click").click(function(){
						var $this = $(this);
						dialog.showConfirmDialog("提交跟进信息", "是否确定提交，提交后将不可修改?", function(){
							submitFinanceFollowInfo($this.parents("tr").attr("followInfoId"));
						});
					});
					
					$("#followInfoDetail .delete").unbind("click").click(function(){
						var $this = $(this);
						dialog.showConfirmDialog("删除跟进信息", "是否确定删除该条跟进信息?", function(){
							deleteFinanceFollowInfo($this.parents("tr").attr("followInfoId"));
						});
					});
					
					$("#followInfoDetail .addFollowInfo").unbind("click").click(function(){
						$("#popdialog").html(html_addFollowInfo);
						$("#dialog_createFinanceFollowInfo input[name=followId]").val(followId);
						dialog.showDialog($("#dialog_createFinanceFollowInfo"));
						
						$("#dialog_createFinanceFollowInfo").on("click", ".visitFile .deleteProductIntro", function() {
							$(this).parent().remove();
							var fileUuid = [];
							for (var j = 0; j < $("#dialog_createFinanceFollowInfo .visitFile .mas").length; j++) {
								fileUuid.push($($("#dialog_createFinanceFollowInfo .visitFile .mas")[j]).attr("fileUuid"));
							}
							$("#dialog_createFinanceFollowInfo input[name=visitFile]").val(fileUuid);
						});
						
						$("#dialog_createFinanceFollowInfo .dialogHandlerBtn").unbind("click").click(function(){
							databind.submitDom($("#dialog_createFinanceFollowInfo"), function(){
								base.alert(1, "添加成功！");
								router.route("client/followInfoDetail.html?followId=" + followId);
								dialog.hideDialog($("#dialog_createFinanceFollowInfo"));
							});
						});
						
						$("#dialog_createFinanceFollowInfo").on("change", "#visitFile", function(){
							tools.uploadFile("visitFile", function(data){
								var str = '<div class="showProductIntroDiv1 visitFile"><a target="_blank" class="mas" fileUuid="' + data.data[0].fileUuid + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + data.data[0].fileUuid + '" class="showProductIntro">'+ tools.inquireFile(data.data[0].fileUuid) +'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a class="deleteProductIntro">x</a></div>';
								$("label[for=visitFile]").before(str);
								var fileUuid = [];
								for (var j = 0; j < $("#dialog_createFinanceFollowInfo .visitFile .mas").length; j++) {
									fileUuid.push($($("#dialog_createFinanceFollowInfo .visitFile .mas")[j]).attr("fileUuid"));
								}
								$("#dialog_createFinanceFollowInfo input[name=visitFile]").val(fileUuid);
							});
						});
						
						//==================================================================================
						
						$("#dialog_createFinanceFollowInfo").on("click", ".visitImage .deleteProductIntro", function() {
							$(this).parent().remove();
							var fileUuid = [];
							for (var j = 0; j < $("#dialog_createFinanceFollowInfo .visitImage .mas").length; j++) {
								fileUuid.push($($("#dialog_createFinanceFollowInfo .visitImage .mas")[j]).attr("fileUuid"));
							}
							$("#dialog_createFinanceFollowInfo input[name=visitImage]").val(fileUuid);
						});
						
						$("#dialog_createFinanceFollowInfo").on("change", "#visitImage", function(){
							tools.uploadFile("visitImage", function(data){
								var str = '<div class="showProductIntroDiv1 visitImage"><a target="_blank" class="mas" fileUuid="' + data.data[0].fileUuid + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + data.data[0].fileUuid + '" class="showProductIntro">'+ tools.inquireFile(data.data[0].fileUuid) +'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a class="deleteProductIntro">x</a></div>';
								$("label[for=visitImage]").before(str);
								var fileUuid = [];
								for (var j = 0; j < $("#dialog_createFinanceFollowInfo .visitImage .mas").length; j++) {
									fileUuid.push($($("#dialog_createFinanceFollowInfo .visitImage .mas")[j]).attr("fileUuid"));
								}
								$("#dialog_createFinanceFollowInfo input[name=visitImage]").val(fileUuid);
							});
						});
						
						//===========================================================================================
						$("#dialog_createFinanceFollowInfo").on("click", ".visitAudio .deleteProductIntro", function() {
							$(this).parent().remove();
							var fileUuid = [];
							for (var j = 0; j < $("#dialog_createFinanceFollowInfo .visitAudio .mas").length; j++) {
								fileUuid.push($($("#dialog_createFinanceFollowInfo .visitAudio .mas")[j]).attr("fileUuid"));
							}
							$("#dialog_createFinanceFollowInfo input[name=visitAudio]").val(fileUuid);
						});
						
						$("#dialog_createFinanceFollowInfo").on("change", "#visitAudio", function(){
							tools.uploadFile("visitAudio", function(data){
								var str = '<div class="showProductIntroDiv1 visitAudio"><a target="_blank" class="mas" fileUuid="' + data.data[0].fileUuid + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + data.data[0].fileUuid + '" class="showProductIntro">'+ tools.inquireFile(data.data[0].fileUuid) +'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a class="deleteProductIntro">x</a></div>';
								$("label[for=visitAudio]").before(str);
								var fileUuid = [];
								for (var j = 0; j < $("#dialog_createFinanceFollowInfo .visitAudio .mas").length; j++) {
									fileUuid.push($($("#dialog_createFinanceFollowInfo .visitAudio .mas")[j]).attr("fileUuid"));
								}
								$("#dialog_createFinanceFollowInfo input[name=visitAudio]").val(fileUuid);
							});
						});
					});
					
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};
	
	//提交跟进信息
	var submitFinanceFollowInfo = function(followInfoId){
		base.fnAjax({
            action: "submitFinanceFollowInfo.json",
            oData: {
            	followInfoId: followInfoId
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	base.alert(1, "提交成功！");
                	router.route("client/followInfoDetail.html?followId=" + followId);
                } else {
                     base.alert(2,"信息获取失败");
                }
            }
        });
	};
	
	//删除跟进信息
	var deleteFinanceFollowInfo = function(followInfoId){
		base.fnAjax({
            action: "deleteFinanceFollowInfo.json",
            oData: {
            	followInfoId: followInfoId
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	base.alert(1, "删除成功！");
                	router.route("client/followInfoDetail.html?followId=" + followId);
                } else {
                     base.alert(2,"信息获取失败");
                }
            }
        });
	};
	
	return {
		init: init
	};
});